package com.example.todolist.service;

import com.example.todolist.entity.TaskEachDay;
import com.example.todolist.mapper.TaskEachDayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;

@Service
public class TaskEachDayService {
    @Autowired
    private TaskEachDayMapper taskEachDayMapper;
    @Autowired
    private HabitService habitService;
    public void setTaskEachDay(Integer userId)
    {
        TaskEachDay taskEachDay = taskEachDayMapper.getTaskEachDay(userId);
        if(taskEachDay == null)
            taskEachDayMapper.setTaskEachDay(userId);
    }

    public void updateTaskEachDay(Integer userId)
    {
        Date date = Date.valueOf(LocalDate.now());
        String weekDay = habitService.getWeekDay(date);
        Integer status = taskEachDayMapper.getWeekStatus(userId,weekDay)+1;
        taskEachDayMapper.updateTaskEachDay(userId,weekDay,status);
    }

    public void resetTaskEachDay(Integer userId)
    {
        taskEachDayMapper.resetTaskEachDay(userId);
    }

    public void deleteTaskEachDay(Integer userId) {
        Date date = Date.valueOf(LocalDate.now());
        String weekDay = habitService.getWeekDay(date);
        Integer status = taskEachDayMapper.getWeekStatus(userId,weekDay)-1;
        taskEachDayMapper.updateTaskEachDay(userId,weekDay,status);
    }
}
